import { Form } from 'antd';
import { useMemo } from 'react';

/**
 * @useDescriptions 表格表单hooks
 * @param options 配置项
 * @returns
 */
export function useDescriptions(options: DescriptionsAPI.DescriptionsProps) {
  // 表单实例
  const [form] = Form.useForm();

  const { schemas, labelWidth = 180, basicForm = form, headerSchema = {}, disabled = false, name = 'description-form' } = options;

  // 遍历出符合规范的数据
  const dataSource = useMemo(() => {
    const tempData: any = [];
    let num = 0;
    schemas.map((item: any) => {
      if (!tempData[num]) tempData[num] = [];
      if (item.component == 'Title') {
        tempData[num].push(item);
        num++;
      } else if (!item.colSpan || item.colSpan == 1) {
        tempData[num].push(item);
        if (tempData[num].length == 2) num++;
      } else {
        tempData[num].push(item);
        num++;
      }
    });
    return tempData;
  }, [schemas]);

  return [
    {
      form: basicForm,
      name,
      dataSource,
      headerSchema,
      labelWidth,
      disabled,
    } as any,
    basicForm,
  ];
}
